package com.bone.common.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


/**
 * @author : WangKeKe
 * @date : 2020/8/13 15:23
 * Description : 统一返回格式 用于展示层
 */

@Data
@ApiModel
public class Result<T> implements Serializable {

    /**
     * 序列化，反序列化验证标识
     **/
    private static final long serialVersionUID = 1L;

    /**
     * 请求成功标识
     **/
    @ApiModelProperty(value = "请求成功标识")
    private boolean flag;

    /**
     * 请求结果信息返回
     **/
    @ApiModelProperty(value = "请求结果")
    private String message;

    /**
     * 返回状态码
     **/
    @ApiModelProperty(value = "返回状态码")
    private Integer code;

    /**
     * 请求数据
     **/
    @ApiModelProperty(value = "请求数据")
    private T record;

    /**
     * 时间戳
     **/
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();


    public Result<T> success(String message) {
        this.flag = true;
        this.message = message;
        this.code = ResultCode.SUCCESS.getCode();
        return this;
    }


    public Result<T> fail(String message) {
        this.flag = false;
        this.message = message;
        this.code = ResultCode.FAILED.getCode();
        return this;
    }


    /* 以下均为 全局 静态方法 使用公共内存空间 且不会被释放一直占用 */

    /**
     * 返回成功
     **/
    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.setFlag(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * 返回成功，自定义数据
     **/
    public static <T> Result<T> ok(T record) {
        Result<T> result = new Result<>();
        result.setFlag(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setRecord(record);
        return result;
    }

    /**
     * 返回成功，自定义返回消息和数据
     **/
    public static <T> Result<T> ok(String message, T record) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setRecord(record);
        result.setFlag(true);
        return result;
    }

    /**
     * 返回错误，自定义信息
     **/
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setMessage(message);
        result.setCode(ResultCode.FAILED.getCode());
        result.setFlag(false);
        return result;
    }

    /**
     * 返回错误，自定义返回码和信息
     **/
    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setFlag(false);
        return result;
    }

    /**
     * token认证失败返回结果
     **/
    public static <T> Result<T> noAuth(String message) {
        return error(ResultCode.UNAUTHORIZED.getCode(), message);
    }

    public static <T> Result<T> noAuth() {
        return error(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage());
    }

    /**
     * 无权限返回结果
     */
    public static <T> Result<T> forbidden(String message) {
        return error(ResultCode.FORBIDDEN.getCode(), message);
    }

    public static <T> Result<T> forbidden() {
        return error(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage());
    }

    /**
     * 参数校验失败返回结果
     */
    public static <T> Result<T> validate() {
        return error(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMessage());
    }

    public static <T> Result<T> validate(String message) {
        return error(ResultCode.VALIDATE_FAILED.getCode(), message);
    }
}
